package com.marvels.approve.manager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.marvels.approve.manager.dao.IOpinionTypeDao;
import com.marvels.approve.manager.dto.OpinionTypeDTO;
import com.marvels.approve.manager.service.IOpinionTypeService;
import com.marvels.common.constants.CaseNodeConstant;
import com.marvels.common.exception.MarvelsBusinessException;
@Service
public class OpinionTypeServiceImpl implements IOpinionTypeService {
	@Autowired
	private IOpinionTypeDao opinionTypeDao;
	@Override
	public List<OpinionTypeDTO> findOpinionTypes(OpinionTypeDTO opinionTypeDTO) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		opinionTypeDTO = opinionTypeDTO == null?new OpinionTypeDTO():opinionTypeDTO;
		PageHelper.startPage(opinionTypeDTO.getPageNum(), opinionTypeDTO.getPageSize());
		List datas = opinionTypeDao.select(opinionTypeDTO);
		return datas;
	}

	@Override
	public OpinionTypeDTO findOpinionType(Long id) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		if(id == null){
			throw new MarvelsBusinessException("请选择查询的类型ID");
		}
		OpinionTypeDTO entity = this.opinionTypeDao.selectByPrimaryKey(id);
		return entity;
	}

	@Override
	public int saveOpinionType(OpinionTypeDTO opinion) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		if(opinion.getId() == null){
			saveNewObject(opinion);
		}else{
			saveOldObject(opinion);
		}
		return 0;
	}
	private int saveNewObject(OpinionTypeDTO opinion)throws MarvelsBusinessException{
		//新增时，检查该编码是否已经存在。
		OpinionTypeDTO opinionTypeDTO = new OpinionTypeDTO();
		opinionTypeDTO.setOpinionTypeCode(opinion.getOpinionTypeCode());
		List datas = this.opinionTypeDao.select(opinionTypeDTO);
		if(datas!=null && datas.size()!=0){
			throw new MarvelsBusinessException(opinion.getOpinionTypeCode()+"已经存在！");
		}
		return this.opinionTypeDao.insert(opinion);
	}
	private int saveOldObject(OpinionTypeDTO opinion)throws MarvelsBusinessException{
		OpinionTypeDTO db = opinionTypeDao.selectByPrimaryKey(opinion.getId());
		if(db==null){
			throw new MarvelsBusinessException(opinion.getOpinionTypeCode()+"不存在，无法更新！");
		}
		return this.opinionTypeDao.updateByPrimaryKeySelective(opinion);
	}

	@Override
	public OpinionTypeDTO findOpinionType(String opinionTypeCode,String nodeCode,String patternCode) throws MarvelsBusinessException {
		// TODO Auto-generated method stub
		OpinionTypeDTO dto = new OpinionTypeDTO();
		dto.setOpinionTypeCode(opinionTypeCode);
		dto.setNodeCode(nodeCode);
		dto.setPatternCode(patternCode);
		List<OpinionTypeDTO> opinionTypes = this.opinionTypeDao.select(dto);
		if(opinionTypes!=null && opinionTypes.size() == 1 ) {
			return opinionTypes.get(0);
		}else {
			throw new MarvelsBusinessException("通过type_code:"+opinionTypeCode+"查询意见类型数据:"+opinionTypes);
		}
	}

	/**将类型做成需要的结构
	 * @return
	 * @throws MarvelsBusinessException
	 */
	public Map<Integer, List<OpinionTypeDTO>> getOpinionTypes(String nodeCode) throws MarvelsBusinessException {
		OpinionTypeDTO  opinionTypeDTO = new OpinionTypeDTO();
		opinionTypeDTO.setNodeCode(nodeCode);
		opinionTypeDTO.setPageSize(99999);
		List<OpinionTypeDTO> ots = findOpinionTypes(opinionTypeDTO);
		if(ots == null ) {
			return null;
		}
		Map<String,OpinionTypeDTO> resultDto = new HashMap();
		Map<String,OpinionTypeDTO> tempDto = new HashMap();
		for(OpinionTypeDTO ot:ots) {
			Integer level = ot.getOpinionLevel();
			String code = ot.getOpinionTypeCode();
			if(level==1 && !resultDto.containsKey(code)) {//顶级
				resultDto.put(code, ot);
			}
			if(!tempDto.containsKey(code)) {//还未放入到其中
				tempDto.put(code, ot);
			}
		}
		//一对多写入
		for(String key : tempDto.keySet()) {
			OpinionTypeDTO opinionType = tempDto.get(key);
			String supOpinionTypeCode = opinionType.getOpinionSupCode();
			Integer level = opinionType.getOpinionLevel();
			if(level!=1) {//非顶级,将其放入到父类型中
				OpinionTypeDTO supOpinionType = tempDto.get(supOpinionTypeCode);
				if(supOpinionType==null) {
					continue;
				}
				List<OpinionTypeDTO> list = supOpinionType.getOpinionTypes();
				if(list == null) {
					list = new ArrayList();
				}
				list.add(opinionType);
				supOpinionType.setOpinionTypes(list);
			}
		}
		//将类型做为key   `opinion_class` tinyint(11) DEFAULT NULL COMMENT '意见分类  0 拒绝  1违例  2复核意见',
		Map<Integer,List<OpinionTypeDTO>> resultIntDto = new HashMap();
		for(String key : resultDto.keySet()) {
			OpinionTypeDTO opinionType = resultDto.get(key);
			Integer opinionClass = opinionType.getOpinionClass();
			List<OpinionTypeDTO> tempList = resultIntDto.get(opinionClass);
			if(tempList == null) {
				tempList = new ArrayList();
			}
			tempList.add(opinionType);
			resultIntDto.put(opinionClass, tempList);
		}
		return resultIntDto;
	}
 
}
